<?php

class MilestoneTable extends Doctrine_Table
{
  public function getWithTasks()
  {
    $selectedProject = sfContext::getInstance()->getUser()->getAttribute('selectedProject');
	
	if($selectedProject == NULL) {
   		 return NULL;
	} 
	
	$q = $this->createQuery('m')
      ->leftJoin('m.Task t')
  	  ->where('m.projectid = ?', $selectedProject->getProjectid())
	  ->orderBy('t.createdate DESC'); 
    return $q->execute();
  }
  
  public function getMilestonesOfSelectedProject()
  {
    $selectedProject = sfContext::getInstance()->getUser()->getAttribute('selectedProject');
    
	if($selectedProject == NULL) {
   		 return NULL;
	} 
	
	$q = $this->createQuery('m')
	  ->where('m.projectid = ?', $selectedProject->getProjectid());
    $milestones = $q->execute();
	if (count($milestones) == 0)
	{
	  sfContext::getInstance()->getUser()->setFlash('error', "Please create Milestone first.", false);
	}
	return $milestones ;
  }
  public function getTasksQuery()
  {
//    $q = Doctrine_Query::create()
//       ->from('Task t');
	return Doctrine_Core::getTable('Task')->addTasksQuery($q);
  }  
    public function getMilestoneTasksQuery($milestoneid)
  {
    $q = $this->createQuery('m')      
	   ->leftJoin('m.Task t')
      ->where('t.milestoneid = ?', $milestoneid);       
	return $q;
  }
	
}